package com.xiaoq.users.service;

import java.net.URI;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.xiaoq.coms.util.JsonUtil;
import com.xiaoq.coms.util.MapUtil;
import com.xiaoq.web.HttpsRestTemplate;

@Component
public class TencentIMService {
	
	private static final String SdkAppid = "1400092469";
	
	private static final String Identifier = "xiaoq";
	
	private static final String UserSig  = "eJxlj11PgzAYhe-5FYRbjSvtimDixcKGkjgXZYtuN00pBV8NH0KLTLP-PsUlNvHcPs-JyfmybNt21nfJBRei1pViat9Ix76yHeSc-8GmgYxxxUib-YNyaKCVjOdKtiN0KaUYIdOBTFYKcjgZA-D63cBd9sbGjd-*9Lsc4KkXmAoUI1wuNmH8MI8fQwgndO9zz5svyDPkM5T24jNNo6ihPZnpKPjQ8jbpiri4PxPaJSR54nGxrVdkEnDqDy*vO56Vm3Vwo8vdyhfLLY3EtTGpoJSnQ9jDnu*jS4P2su2grkYBI5e6mKCfONbBOgLGZVxq";
	
	
	private static final String BaseUrl = "https://console.tim.qq.com/";
	
	private static final String accountUrl = "v4/im_open_login_svc/account_import";
	
	private static final String multiAccountUrl = "v4/im_open_login_svc/multiaccount_import";
	
	private static final Logger LOG = LoggerFactory.getLogger(TencentIMService.class);
	
	@Autowired
	private HttpsRestTemplate rest;
	
	@Async
	public  TencentResponseVo tencentAccount(TencentRequestVo request){
		String identifier = request.getIdentifier();
		String nick = request.getNick();
		if(StringUtils.isBlank(nick)){
			nick = identifier;
		}
		String faceUrl = request.getFaceUrl();
		if(StringUtils.isBlank(faceUrl)){
			faceUrl = "http://p0sd9v5tn.bkt.clouddn.com/down2.png";
		}
		String type = request.getType();
		Map<String, Object> body = MapUtil.buildMapKVGen(
				new Object[][] { { "Identifier", identifier }, 
					{"Nick", nick },
					{"FaceUrl", faceUrl}}, String.class,
				Object.class);
		
		String requestUrl = BaseUrl + accountUrl + "?usersig=" + UserSig + "&apn=1&identifier="+Identifier+"&sdkappid="+SdkAppid+"&contenttype=json";
		String respStr = post(requestUrl, body);
		LOG.info("腾讯IM注册结果"+respStr);
		return JsonUtil.asObject(respStr, TencentResponseVo.class);
	}
	
	@Async
	public  TencentResponseVo tencentMultiAccount(TencentRequestVo request){
		List<String> accounts = request.getAccounts();
		Map<String, Object> body = MapUtil.buildMapKVGen(
				new Object[][] { { "Accounts", accounts }}, String.class,
				Object.class);
		
		String requestUrl = BaseUrl + multiAccountUrl + "?usersig=" + UserSig + "&apn=1&identifier="+Identifier+"&sdkappid="+SdkAppid+"&contenttype=json";
		LOG.info("请求地址"+requestUrl);
		String respStr = post(requestUrl, body);
		LOG.info("腾讯IM注册返回结果"+respStr);
		return JsonUtil.asObject(respStr, TencentResponseVo.class);
	}
	
	
	private  String post(String url, Map<String, Object> body) {
		HttpEntity<Map<String, Object>> reqEntity = new HttpEntity<>(body);
		String respStr = null;
		try {
			respStr = rest.postForObject(new URI(url),
					reqEntity,
					String.class);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
		if (respStr != null) {
			return respStr;
		}
		return respStr;
	}
}
